//
//  YKQAddNameViewController.m
//  YaoKongQi
//
//  Created by ryou on 2021/5/19.
//

#import "YKQAddNameViewController.h"
#import "YKQAddViewController.h"

@interface YKQAddNameViewController ()<UITextFieldDelegate,BUNativeExpressRewardedVideoAdDelegate>
@property(nonatomic,assign)NSInteger ykqloadTag;
@end

@implementation YKQAddNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _ykqloadTag=0;
    self.title = @"添加设备名称";
    
    UIImageView *headView=[[UIImageView alloc]initWithFrame:CGRectMake(20, 100, kYKQWidth - 40, (kYKQWidth - 40) *9/16)];
    headView.layer.cornerRadius = 10;
    headView.image = [UIImage imageNamed:[NSString stringWithFormat:@"ykq_icon_%ld",self.index]];
    headView.contentMode=UIViewContentModeScaleAspectFit;
    headView.clipsToBounds=YES;
    headView.backgroundColor=kBlackColor;
    [self.view addSubview:headView];
    
    UIImageView *bgImage=[[UIImageView alloc]initWithFrame:CGRectMake(20, headView.frame.size.height + headView.frame.origin.y + 20, kYKQWidth - 40, 74)];
    bgImage.image=[UIImage imageNamed:@"ykq_icon_white_bg.png"];
    bgImage.contentMode=UIViewContentModeScaleToFill;
    [self.view addSubview:bgImage];
    bgImage.userInteractionEnabled=YES;
    
    UITextField *addname=[[UITextField alloc]initWithFrame:bgImage.bounds];
    addname.placeholder = @"请输入设备名称";
    addname.delegate=self;
    addname.returnKeyType=UIReturnKeyDone;
    addname.textAlignment=NSTextAlignmentCenter;
    addname.font = [UIFont boldSystemFontOfSize:20];
    [bgImage addSubview:addname];
    
    UIButton *doneBtn=[[UIButton alloc]initWithFrame:CGRectMake(20, bgImage.frame.size.height + bgImage.frame.origin.y + 20, kYKQWidth - 40, 74)];
    [doneBtn  setBackgroundImage:[UIImage imageNamed:@"ykq_icon_white_bg.png"] forState:UIControlStateNormal];
    [doneBtn addTarget:self action:@selector(doneBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    [doneBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    doneBtn.titleLabel.font=[UIFont boldSystemFontOfSize:25];
    [self.view addSubview:doneBtn];
    
    [self ykqLoadRewardVideoAd];
    [self ykqBUAdLoadBanner];

    // Do any additional setup after loading the view.
}

-(void)doneBtnClick{
    [self ykqShowRewardVideoAd];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
   
    [self.view endEditing:YES];
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)ykqLoadRewardVideoAd{
 
    BURewardedVideoModel *model = [[BURewardedVideoModel alloc] init];
    self.ykqReWardVideo = [[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:kYkqBuadJili rewardedVideoModel:model];
    self.ykqReWardVideo.delegate = self;
    [self.ykqReWardVideo loadAdData];
    
}

- (void)ykqShowRewardVideoAd {
    if (self.ykqReWardVideo && self.ykqloadTag == 1 ) {
        [[YKQAlertView sharedInstance]showAlertWithWithTag:^{
          
            [self.ykqReWardVideo showAdFromRootViewController:self];
      
        }];

    }else if(self.ykqloadTag == 2){
        [self ykqShowaddViewController];
    }
}
-(void)ykqShowaddViewController{
    self.ykqloadTag = 0 ;
    YKQAddViewController *add=[[YKQAddViewController alloc]init];
    [self.navigationController pushViewController:add animated:YES];
    [self  ykqLoadRewardVideoAd];
}

#pragma mark - BUNativeExpressRewardedVideoAdDelegate

- (void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
    self.ykqloadTag = 2;
}


- (void)nativeExpressRewardedVideoAdDidDownLoadVideo:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    self.ykqloadTag = 1;
}
 
- (void)nativeExpressRewardedVideoAdDidPlayFinish:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
    [self ykqShowaddViewController];
}
- (void)nativeExpressRewardedVideoAdDidClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    self.ykqloadTag = 0 ;
    [self  ykqLoadRewardVideoAd];
}
@end
